package com.factory.test.m5622;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ScrollView;

import androidx.databinding.DataBindingUtil;

import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.databinding.ActivityM5622SerialPortBinding;
import com.yecontech.sdk.base.io.IOCallBack;
import com.yecontech.sdk.base.io.SerialPortManager;

/**
 * 串口测试
 */
public class SerialPortActivity extends BaseTestActivity {
    private ActivityM5622SerialPortBinding binding;
    private SerialPortManager serialPortManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_m5622_serial_port);
        binding.setViewClick(new ViewClick());


        binding.serialPortList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                binding.node.setText("/dev/" + parent.getItemAtPosition(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    public void appendMessage(String s){
        handler.post(new Runnable() {
            @Override
            public void run() {
                binding.textView.append(s);
                binding.textView.append("\n");
            }
        });

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        }, 100);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(serialPortManager != null) {
            serialPortManager.close();
            serialPortManager = null;
        }
    }


    public class ViewClick{
        public void onOpen(){
            String node = binding.node.getText().toString();
            String br = binding.baudRate.getText().toString();

            if(isEmpty(node, br)){
                showWarning("请输入节点及波特率");
                return;
            }

            binding.node.setEnabled(false);
            binding.baudRate.setEnabled(false);
            binding.open.setEnabled(false);
            serialPortManager = sdkService.getSerialPortManager();
            serialPortManager.open(node, Integer.parseInt(br), new IOCallBack() {
                @Override
                public void onReceiverData(byte[] by) {
                    appendMessage(new String(by));
                }
            });
        }

        public void onClose(){
            if(serialPortManager != null){
                serialPortManager.close();
                serialPortManager = null;
            }

            binding.node.setEnabled(true);
            binding.baudRate.setEnabled(true);
            binding.open.setEnabled(true);
        }

        public void onSend(){
            if(serialPortManager == null || !serialPortManager.isOpen()){
                showWarning("请先打开串口");
                return;
            }


            String s = binding.input.getText().toString();
            if(isEmpty(s)){
                showWarning("请输入16进制数据");
                return;
            }

            if(s.length() % 2 != 0){
                showError("仅支持收发16进制");
                return;
            }
            serialPortManager.send(s.getBytes());
        }

        public void onClear(){
            binding.textView.setText("");
        }
    }
}
